package com.smedi.ismedi.personnel.core.domain.service.impl;

import cn.hutool.core.util.IdUtil;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.smedi.ismedi.personnel.core.domain.aggregatemodel.Signature;
import com.smedi.ismedi.personnel.core.domain.repository.SignatureRepository;
import com.smedi.ismedi.personnel.core.domain.repository.SignatureTemporaryRepository;
import com.smedi.ismedi.personnel.core.domain.service.SignatureDomainService;
import com.smedi.ismedi.personnel.core.infrastructure.constant.ConstantField;
import com.smedi.ismedi.personnel.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-6-9 14:00
 */
@Slf4j
@Service
@AllArgsConstructor
public class SignatureDomainServiceImpl implements SignatureDomainService {
    private final SignatureRepository signatureRepository;

    private final SignatureTemporaryRepository signatureTemporaryRepository;

    @Override
    public Signature saveSignature(String signatureUUID, Long personId) {
        boolean exist = true;
        Signature signature = signatureRepository.selectSignatureByPersonId(personId);
        if (signature == null) {
            signature = new Signature().setPersonId(personId);
            exist = false;
        }

        // 如果不是一致的id，说明被更新过了，则执行保存数据
        if (!signatureUUID.equals(signature.getUuid())) {
            String tempPathPattern = ConstantField.signatureTempPath + signatureUUID; // 临时文件地址

            // 获取服务器上存储的路径
            List<String> keys = signatureTemporaryRepository.checkSignatureTemporaryIsExist(tempPathPattern);
            if (keys.size() > 0) {
                String key = keys.get(0);
                StorePath storePath = signatureTemporaryRepository.getStorePathByKey(key);
                if (storePath != null) {
                    signature.setFileGroup(storePath.getGroup())
                            .setFilePath(storePath.getPath())
                            .setUuid(signatureUUID);

                    signature = exist ? signatureRepository.doUpdate(signature) : signatureRepository.doInsert(signature);
                    signatureTemporaryRepository.deleteSignatureTemporary(key);
                } else {
                    throw LogicException.raise(CustomError.SIGNATURE_UUID_MISS);
                }
            } else {
                throw LogicException.raise(CustomError.SIGNATURE_UUID_MISS);
            }
        }

        return signature;
    }

    @Override
    public Signature saveSignature(StorePath storePath, Long personId) {
        boolean exist = true;
        // 判断当前人员是否有照片
        Signature signature = signatureRepository.selectSignatureByPersonId(personId);
        if (signature == null) {
            signature = new Signature().setPersonId(personId);
            exist = false;
        }

        signature.setFileGroup(storePath.getGroup())
                .setFilePath(storePath.getPath())
                .setUuid(IdUtil.simpleUUID());

        signature = exist ? signatureRepository.doUpdate(signature) : signatureRepository.doInsert(signature);
        return signature;
    }

    @Override
    public Signature getSignature(String signatureUUID) {
        Signature signature = signatureRepository.selectSignatureBySignatureUUID(signatureUUID);
        if (signature != null) {
            return signature;
        } else {
            throw LogicException.raise(CustomError.SIGNATURE_NOT_EXIST);
        }
    }

    @Override
    public Signature getSignatureByPersonId(Long personId) {
        Signature signature = signatureRepository.selectSignatureByPersonId(personId);
        if (signature != null) {
            return signature;
        } else {
            throw LogicException.raise(CustomError.SIGNATURE_NOT_EXIST);
        }
    }
}
